package stbm.objects;

import java.util.LinkedList;

/**
 * This class represents the ship.
 * 
 * @author Marten
 * @author Nirav
 * 
 */
public class Ship {

	public String type;
	public int tank; // Amount of fuel in the tank.
	public static final int MILES_PER_GALLON = 5;
	public static double GALLONS_PER_MILE = 1.0 / MILES_PER_GALLON;
	/*
	 * In 1 gallon, the ship can go upto 5 miles; So 1 mile requires 1/5 gallon
	 */
	public LinkedList<TradeGood> tg;
	public int cargoLimit;

	/*
	 * Constructor for the ship
	 * 
	 * @param type  - type of ship
	 */
	public Ship(String type) {
		this.type = type;
		this.tank = 100;
		tg = new LinkedList<TradeGood>();
		setCargoLimit();
	}

	/*
	 * Creates a new ship
	 */
	public Ship() {
		this("Gnat");
	}

	public void setCargoLimit() {

		/*
		 * switch (type) { case "Gnat": this.cargoLimit = 100; }
		 */

		this.cargoLimit = 10;

	}

	/**
	 * Returns the amount of cargo space on this ship
	 * 
	 * @return cargoCount, the amount of cargo space
	 */
	public int getCargoCount() {
		return tg.size();
	}

	/**
	 * Adds cargo to the ship
	 * 
	 */
	public void addCargo(TradeGood e) {

		tg.add(e);

	}

	/**
	 * Removes cargo from the ship
	 * 
	 */
	public void removeCargo(TradeGood e) {

		tg.remove(e);

	}

	public LinkedList<TradeGood> getCargo() {
		return this.tg;
	}

	/**
	 * Returns the amount of fuel left in the tank
	 * 
	 * @return an int with the amount of fuel left
	 */
	public int getTank() {
		return tank;
	}

	/*
	 * Adding gas to the tank
	 * 
	 * @param liters amount of gas added
	 */
	public void addToTank(int litres) {

		this.tank += litres;

	}

	/**
	 * Travels to destination from a source
	 * @param source the source planet
	 * @param destination the destination planet
	 * @return true/false based on whether ship can travel or not
	 */
	public boolean travel(Planet source, Planet destination) {

		int distanceBetweenPlanets = Math.abs(destination.distanceFromCenter
				- source.distanceFromCenter);
		double tankRequired = distanceBetweenPlanets * GALLONS_PER_MILE;
		if (tankRequired > this.tank) {
			return false;
		}
		double fuelExpended = distanceBetweenPlanets * GALLONS_PER_MILE;
		this.tank -= fuelExpended;
		return true;

	}

	/**
	 * Takes away 5 gallons from fuel
	 */
	public void alienAttack() {

		this.tank -= 5;
		correctFuel();
	}

	/**
	 * Adds 10 gallons to fuel
	 */
	public void freeFuel() {
		this.tank += 10;
	}

	/**
	 * Resets fuel to 0 if it becomes negative
	 */
	public void correctFuel() {

		if (this.tank < 0) {
			this.tank = 0;
		}

	}
}
